package com.xiaoluo.lottery.common.asyn;

import com.xiaoluo.lottery.common.utils.ApplicationContextUtil;
import com.xiaoluo.lottery.common.weixin.base.IPaidCallBack;
import com.xiaoluo.lottery.entity.Order;
import com.xiaoluo.lottery.exception.LotteryException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * Project : lottery
 * Package : com.xiaoluo.lottery.common.asyn
 * Created by Willian.
 * Date  : 2016/4/13 19:37.
 */
public class Monitor implements Runnable {
    private final static Logger logger = LoggerFactory.getLogger(Monitor.class);

    private final static int MAX_THREADS = 30;

    private ThreadPoolExecutor executorService =  new ThreadPoolExecutor(MAX_THREADS, MAX_THREADS,0L, TimeUnit.MILLISECONDS,new LinkedBlockingQueue<Runnable>());

    @Override
    public void run() {
        while (true){

            while (!AsynQueue.queue.isEmpty()){
                if(executorService.getActiveCount()<MAX_THREADS-1){
                    Order obj = AsynQueue.queue.poll();
                    logger.info("poll order:{}",obj.getPrepayId());
                    executorService.execute(new Worker(obj));
                }

            }
            //
            try {
                Thread.sleep(10*100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

        }
    }

}
